package com.crack.nga2.span;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.SpannableStringBuilder;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.crack.nga2.R;
import com.crack.nga2.utils.GlobalConst;


/**
 * 图片异步加载
 * Created by Administrator on 2016/1/4.
 */
public class ImageGetterImp  {
   /* private static Drawable defaultDrawable;
    private ImageLoader imageLoader;
    private TextView textView;
    private Object tag;
    private SpannableStringBuilder spannable;
    private Context context;

    public ImageGetterImp(Context context, TextView textView, SpannableStringBuilder spannable) {
        this.textView = textView;
        this.spannable = spannable;
        this.context = context;
        tag = textView.getTag();
        imageLoader = ImageLoader.getInstance();
        if (defaultDrawable == null) {
            defaultDrawable = context.getResources().getDrawable(R.drawable.loading_img);
            int width = defaultDrawable.getIntrinsicWidth();
            float f = width * 1.0f / GlobalConst.SCREEN_WIDTH;
            width = GlobalConst.SCREEN_WIDTH;
            int height = (int) (defaultDrawable.getIntrinsicHeight() / f);
            defaultDrawable.setBounds(0, 0, width, height);
        }
    }


    @Override
    public Drawable getDrawable(String source) {
        if (source != null && source.startsWith("./")) {
            source = GlobalConst.IMG_ATTACH_URL + source.substring(1);
        }
        Bitmap bitmap;
        Drawable drawable = new BitmapDrawable(context.getResources(),bitmap = imageLoader.getMemoryCache().get(source));
        if (bitmap != null) {
            return drawable;
        } else {
            loadImage(source);
            return defaultDrawable;
        }
    }



    private void loadImage(String source) {
        ImageSize imageSize = new ImageSize(150, 150);
        imageLoader.loadImage(source, imageSize, this);
    }

    @Override
    public void doClick(View view, String source) {

    }

    @Override
    public void onLoadingStarted(String s, View view) {
        Log.i("tag", s + "开始下载");
    }

    @Override
    public void onLoadingFailed(String s, View view, FailReason failReason) {

    }

    @Override
    public void onLoadingComplete(String s, View view, Bitmap bitmap) {
        if (textView.getTag() == tag) {
            SpanUtil.imageSpan(spannable, this);
            textView.setText(spannable);
        }
    }

    @Override
    public void onLoadingCancelled(String s, View view) {

    }*/
}
